import { createApp, reactive, watch, ref } from "vue";
// import { get } from "lodash";

import en from "./en"; //国际化文件 英文
import zhCn from "./zh-cn"; //国际化文件 中文
// console.log(zhCn);
let i18nStore = reactive({
  t(path, option) {
    // console.log(this);
    let languages = this.languages || {};
    let locale = languages[this.language];
    return this.i18n(path, option, locale);
  },
  i18n(path, option, locale) {
    let v = get(locale, path, path);
    return typeof v == "string"
      ? v.replace(/\{(\w+)\}/g, (_, key) => {
          var _a;
          return `${(_a = option == null ? void 0 : option[key]) != null ? _a : `{${key}}`}`;
        })
      : v;
  },
  language: null,
  languages: {
    en,
    "zh-CN": zhCn
  },
  setLanguage(language, locale) {
    this.language = language || navigator.language || navigator.userLanguage;
    if (locale) {
      this.languages = this.languages || {};
      this.languages[language] = locale;
    }
  }
});
i18nStore.setLanguage(); //初始化默认语言
function i18n() {
  return i18nStore.t(...arguments);
}
i18n.store = i18nStore;
i18n.setLanguage = function () {
  i18nStore.setLanguage(...arguments);
};
window.i18n = i18n;
export default i18n;
// export { i18n };

// ==========================抽离lodash.get====================================================
var reIsDeepProp = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
  reIsPlainProp = /^\w*$/,
  rePropName = /[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g;
/**
 * Checks if `value` is a property name and not a property path.
 *
 * @private
 * @param {*} value The value to check.
 * @param {Object} [object] The object to query keys on.
 * @returns {boolean} Returns `true` if `value` is a property name, else `false`.
 */
function isKey(value, object) {
  if (Array.isArray(value)) {
    return false;
  }
  var type = typeof value;
  if (type == "number" || type == "symbol" || type == "boolean" || value == null || typeof value == "symbol") {
    return true;
  }
  return reIsPlainProp.test(value) || !reIsDeepProp.test(value) || (object != null && value in Object(object));
}
// 1、主方法 get
function get(object, path, defaultValue) {
  // 判断object是否为null,如果不是，调用baseGet方法获取object的path路径下的值
  const result = object == null ? undefined : baseGet(object, path);
  // 若结果是undefined，则返回默认值
  return result === undefined ? defaultValue : result;
}
// 2、baseGet方法 获取对象路径下的值
function baseGet(object, path) {
  // castPath方法获取object的path
  path = castPath(path, object);
  let index = 0;
  const length = path.length;

  //如果object不为null且当前index < 路径的长度时，获取object的值,更新object
  while (object != null && index < length) {
    object = object[toKey(path[index++])]; // 将path数组中的值转为key
  }
  // 最后一级，返回object或者返回 undefined
  return index && index == length ? object : undefined;
}

// 以上涉及两个方法：（1）将castPath 获取 object中的path，转为数组
// （2）toKey 将path数组中对应的值转为key
// (1）castPath（path, object） 如castPath('a[b].c', object)
function castPath(value, object) {
  if (Array.isArray(value)) {
    return value; // 如果value是个数组，直接返回
  }
  // console.log(stringToPath(value))
  return isKey(value, object) ? [value] : stringToPath(value);
  // 其中，isKey方法判断value是不是object对象的获取key的合法路径，主要基于value的类型判断和正则匹配，具体可见[lodash的解释](https://github.com/Liusq-Cindy/lodash/blob/e0029485ab4d97adea0cb34292afb6700309cf16/.internal/isKey.js#L15)
  // stringTopath方法，则是将字符串形式的路径，转化为数组，如将'a[0].c.d' 利用正则和字符串api转化为 ['a',0,'c','d'],具体可见[github](https://github.com/Liusq-Cindy/lodash/blob/e0029485ab4d97adea0cb34292afb6700309cf16/.internal/isKey.js#L15)
}

//（2）toKey(path[index++]) 将path数组中对应的值转为key
const INFINITY = 1 / 0;
function toKey(value) {
  // 如果数组中取到的key是字符串或isSymbol为true,则返回字符串
  if (typeof value === "string" || isSymbol(value)) {
    return value;
  }
  // 此外，判断+-0，或者返回value本身，如纯数字
  const result = `${value}`;
  return result == "0" && 1 / value == -INFINITY ? "-0" : result;
}
var stringToPath = function (string) {
  var result = [];
  if (string.charCodeAt(0) === 46 /* . */) {
    result.push("");
  }
  string.replace(rePropName, function (match, number, quote, subString) {
    result.push(quote ? subString.replace(reEscapeChar, "$1") : number || match);
  });
  return result;
};
